package com.aperos.android15;

import java.util.List;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;

public class AperosPasChers extends MapActivity implements Runnable {
	public Context context;
	public static MyLocationOverlay myLocationOverlay;
	private MapController mapController;
	public static List<Overlay> mapOverlays;
	private MapView mapView;
	private ProgressDialog dialog;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        context = this;
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        
        
        
        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mapOverlays = mapView.getOverlays();
        mapController = mapView.getController();
        myLocationOverlay = new MyLocationOverlay(this, mapView);
        
        initOverlays();
        

    }
    
    private void initOverlays() {
    	try {
    		mapOverlays.removeAll(mapView.getOverlays());
    		dialog = ProgressDialog.show(this, "", getString(R.string.dialog_loading_bar_list), true);
    		new Thread(this).start();
    		
    		
			
	        mapOverlays.add(myLocationOverlay);
	        myLocationOverlay.enableMyLocation();
	        myLocationOverlay.runOnFirstFix(new Runnable() {
	            public void run() {
	            	try {
	            		mapController.animateTo(myLocationOverlay.getMyLocation());
	            		mapController.setZoom(15);
	            	} catch (Exception e) {
	            		e.printStackTrace();
	            	}
	            }
	        });

    	} catch (Exception e) {
			e.printStackTrace();
		}
    }
    

    
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case 1:
                return new ProgressDialog(this, ProgressDialog.STYLE_SPINNER);
        }
        return null;
    }
   
    public View getInfosView() {
    	return (View) findViewById(R.layout.view_info);
    }
    

	@Override
	protected boolean isRouteDisplayed() {
		 return false;
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menu, menu);
	    return true;
	}
	
	/**
	 * Action des boutons du menu
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case R.id.menu_add_bar:  
	        	if (myLocationOverlay.getMyLocation() != null) { 
	        		Intent intent = new Intent(this, BarEdit.class);
	        		this.startActivity(intent);
	        	} else {
	        		Toast.makeText(this, this.getString(R.string.error_dialog_text), 1000).show();
	        	}
	        	break;
	        case R.id.menu_my_position: 
	        	myLocationOverlay.runOnFirstFix(new Runnable() {
	                public void run() {
	                	try {
	                		mapController.animateTo(myLocationOverlay.getMyLocation());
	                		mapController.setZoom(18);
	                	} catch (Exception e) {
	                		e.printStackTrace();
	                	}
	                }
	            });
	        	break;
	        case R.id.menu_init_list:
	        	initOverlays();
	        	break;
	    }
	    return true;
	}

	@Override
	public void run() {
		try {
	        Service.initListBars(context);
			if (Service.listBars .size() > 0) {
	        	Drawable cheapItem = context.getResources().getDrawable(R.drawable.beer_mug_yellow_36p);
	        	Drawable middleItem = context.getResources().getDrawable(R.drawable.beer_mug_orange_36p);
	        	Drawable expensiveItem = context.getResources().getDrawable(R.drawable.beer_mug_red_36p);
	        	Drawable cheapItemHH = context.getResources().getDrawable(R.drawable.beer_mug_yellow_hh_36p);
	        	Drawable middleItemHH = context.getResources().getDrawable(R.drawable.beer_mug_orange_hh_36p);
	        	Drawable expensiveItemHH = context.getResources().getDrawable(R.drawable.beer_mug_red_hh_36p);
	        	
	        	BarItems cheapBarItems = new BarItems(cheapItem, context);
	        	BarItems middleBarItems = new BarItems(middleItem, context);
	        	BarItems expensiveBarItems = new BarItems(expensiveItem, context);
	        	BarItems cheapBarItemsHH = new BarItems(cheapItemHH, context);
	        	BarItems middleBarItemsHH = new BarItems(middleItemHH, context);
	        	BarItems expensiveBarItemsHH = new BarItems(expensiveItemHH, context);
	        	
	        	
	        	for (Bar bar : Service.listBars) {
	        		if (bar.isInHappyHour()) {
	        			if (bar.getPriceBeerHappyHour() <= 3.5) {
		        			cheapBarItemsHH.addOverlay(bar);
		        		} else if (bar.getPriceBeerHappyHour() <= 4.5) {
		        			middleBarItemsHH.addOverlay(bar);
		        		} else {
		        			expensiveBarItemsHH.addOverlay(bar);
		        		}
	        		} else {
		        		if (bar.getPriceBeer() <= 3.5) {
		        			cheapBarItems.addOverlay(bar);
		        		} else if (bar.getPriceBeer() <= 4.5) {
		        			middleBarItems.addOverlay(bar);
		        		} else {
		        			expensiveBarItems.addOverlay(bar);
		        		}
	        		}
	    		}
	    	
	        	if (cheapBarItems.size() > 0) {
	        		mapOverlays.add(cheapBarItems);
	        	}
				if (middleBarItems.size() > 0) {
					mapOverlays.add(middleBarItems);		
				}
				if (expensiveBarItems.size() > 0) {
					mapOverlays.add(expensiveBarItems);
				}
				if (cheapBarItemsHH.size() > 0) {
					mapOverlays.add(cheapBarItemsHH);
				}
				if (middleBarItemsHH.size() > 0) {
					mapOverlays.add(middleBarItemsHH);
				}
				if (expensiveBarItemsHH.size() > 0) {
					mapOverlays.add(expensiveBarItemsHH);
				}
	        	
	        }
			mHandler.post(mUpdateResults);
		 } catch (Exception e) {
		    e.printStackTrace();
		    mHandler.post(mUpdateError);
		 }
	}
	
	final Handler mHandler = new Handler();

    final Runnable mUpdateResults = new Runnable() {
        public void run() {
        	dialog.dismiss();
        	updateMapView();
        }
    };
    
    final Runnable mUpdateError = new Runnable() {
        public void run() {
        	dialog.dismiss();
        	displayError();
        }
    };
    
    public void updateMapView() {
		mapController.zoomOut();
		mapController.zoomIn();
	}
    
	public void displayError() {
		Toast.makeText(context, R.string.error_get_bars_list, 2000).show();
	}
	
}

